package com.zhouxiaoge.antlr4.csv;


import com.zhouxiaoge.antlr4.calc.LabeledExprLexer;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.FileInputStream;
import java.io.InputStream;

public class LoadCSV {


    public static void main(String[] args) throws Exception {
        String csv = "Details,Month,Amount\n" +
                "Mid Bonus,June,\"$2,000\"\n" +
                ",January,\"zippo\"\n" +
                "Total Bonuses,\"\",\"$5,000\"\n";
        CharStream charStream = CharStreams.fromString(csv);
        CSVLexer lexer = new CSVLexer(charStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CSVParser parser = new CSVParser(tokens);
        parser.setBuildParseTree(true); // tell ANTLR to build a parse tree
        ParseTree tree = parser.file();

        ParseTreeWalker walker = new ParseTreeWalker();
        Loader loader = new Loader();
        walker.walk(loader, tree);
        System.out.println(loader.rows);
        /*
            [
                {Details=Mid Bonus, Month=June, Amount="$2,000"},
                {Details=, Month=January, Amount="zippo"},
                {Details=Total Bonuses, Month="", Amount="$5,000"}
            ]
         */
    }
}
